import { FormSchema } from '@/components/Form';
import { isArray } from '@/utils/is';
const QUERY_SEPTOR = '_OP_';

export function transQueryFields(
  formValues: Recordable,
  formSchemas: FormSchema[]
) {
  const defultTransDate = (date: any) => {
    return date?.format?.('YYYY-MM-DD HH:mm:ss') ?? date;
  };
  // 
  formSchemas.forEach((item) => {
    if (item.queryOption) {
      if (item.component === 'RangePicker') {
        if (formValues[item.field]) {
          let dateArr = [];
          if (isArray(formValues[item.field])) {
            dateArr = formValues[item.field].map((item) =>
              defultTransDate?.(item)
            );
          } else {
            dateArr = formValues[item.field].split(',');
          }
          formValues[
            `${item.field}${QUERY_SEPTOR}${item.queryOption}`
          ] = `${dateArr[0]},${dateArr[1]}`;
        }
      } else {
        formValues[`${item.field}${QUERY_SEPTOR}${item.queryOption}`] =
          formValues[item.field];
      }
      if (item.component === 'DatePicker') {
        formValues[`${item.field}${QUERY_SEPTOR}${item.queryOption}`] =
          formValues[item.field]?.format?.('YYYY-MM-DD');
      }
    } else {
      formValues[`${item.field}${QUERY_SEPTOR}eq`] = formValues[item.field];
    }
    // delete formValues[item.field];
  });
  return formValues;
}
